<template>
  <div class="take-number-area" data-info="自助取号" v-loading="loading">
    <el-button class="next" @click="handleNext" type="primary">
      下一步{{ stepInfo }}
    </el-button>
    <div class="step-comps-area" data-info="具体的业务">
      <!--  handleShown:弹层呼出 -->
      <!--  handlePatientInfoSure:患者确定 -->
      <!--  handleSureReservationInfo:预约信息确定取号 -->
      <component
          :is="curCompName"
          @handlePopup="handleShown"
          @handlePatientInfoSure="handlePatientInfoSure"
          @handleSureReservationInfo="handleSureReservationInfo"
      />
    </div>
    <!--底部操作-->
    <PatientNumberOperate :key="widget" @handleBack="handlePre"/>

    <bizCancelSure
        :info="takeNumberInfo.info"
        :visible="takeNumberInfo.visible"
        @handleCancel="handleCancel"
        @handleSure="handleSureCancel"
    />
  </div>
</template>

<script>

import {v4 as uuid} from "uuid";
import CardType from '../comps/cardType';
import MedInsCode from '../comps/medInsCode';
import PatientInfo from '../comps/patientInfo';
import RegisterInfo from '../comps/reservationInfo';
import PatientNumberRezResult from "@sc/DRCbiz/comps/patientNumberRezResult";
import PatientNumberOperate from "@sc/DRCbiz/comps/patientNumberOperate/index.vue";
import BizCancelSure from "@c/public/layer/scenes/bizCancelSure/index.vue";
import {buildParentApi} from "@/utils/designer/util";
import {getCardTypeInfo} from "@sc/DRCbiz/store/getters";

let buildParent = buildParentApi();

export default {
  name: 'TakeNumber',

  componentName: "TakeNumber",

  components: {
    CardType,
    MedInsCode,
    PatientInfo,
    RegisterInfo,
    BizCancelSure,
    PatientNumberOperate,
    PatientNumberRezResult,
  },

  data() {
    return {
      widget: uuid(),
      takeNumberInfo: {
        info: Object(),
        visible: false
      },
      curCompName: 'CardType',
      compsOptions: {
        1: 'CardType',
        2: 'MedInsCode',
        3: 'PatientInfo',
        4: 'RegisterInfo',
        5: 'PatientNumberRezResult',
      },
    }
  },

  computed: {
    // 展示底部组件
    shownOperate() {
      return this.stepInfo !== 1;
    },

    // 步骤
    stepInfo() {
      return this.$store.getters['DRCbiz/getStepsInfo']
    },

    //获取Loading状态
    loading() {
      return this.$store.getters['layout/getLoading']
    },

    //选中的卡片信息
    cardTypeInfo() {
      return this.$store.getters['DRCbiz/getCardTypeInfo']
    },

    // 当前患者
    patientInfo() {
      return this.$store.getters['DRCbiz/getPatientInfo']
    },

  },

  mounted() {
  },

  methods: {
    // 下一步
    async handleNext() {
      let stepInfo = parseInt(this.stepInfo)
      this.widget = uuid();
      if (stepInfo === 5) {
        await this.$store.dispatch('DRCbiz/setStepsInfo', 1)
      } else {
        await this.$store.dispatch('DRCbiz/setStepsInfo', ++stepInfo)
      }
    },

    // 返回上一层&首页
    async handlePre() {
      this.widget = uuid();
      let stepInfo = parseInt(this.stepInfo);
      if (stepInfo === 1) {
        await this.$store.dispatch('DRCbiz/setStepsInfo', 1)
        await this.$router.push({
          path: `/apptsReg/2`
        });
      } else {
        await this.$store.dispatch('DRCbiz/setStepsInfo', --stepInfo);
      }
      switch (parseInt(this.stepInfo)) {
        case 2:
          // 重置电子凭证-防止返回后监听电子凭证跳回当前页
          let cardTypeInfo = (this.cardTypeInfo);
          Reflect.set(cardTypeInfo, 'cardNo', null);
          await this.$store.dispatch('DRCbiz/setCardTypeInfo', cardTypeInfo);
          buildParent.executeParentMethod('openScan');
          break
      }
    },

    // 患者信息确认--获取预约记录列表
    handlePatientInfoSure() {
      const self = this;
      let params = Object();
      let paramInfo = Object();
      let cardTypeInfo = this.cardTypeInfo;
      let patientInfo = this.patientInfo;
      let devInfo = JSON.parse(localStorage.getItem('deviceInfo'));
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'QueryRegisterRecord');

      if (patientInfo.state) {
        Reflect.set(paramInfo, 'patientId', (patientInfo.result.patientId));
      } else {
        Reflect.deleteProperty(paramInfo, 'patientId');
      }
      Reflect.set(paramInfo, 'startDate', this.$day().format('YYYY-MM-DD'));
      // Reflect.set(params, 'endDate', this.$day().format('YYYY-MM-DD'));
      Reflect.set(paramInfo, 'endDate', '2025-03-21');
      // 1:预约 2:挂号3:加号 4:诊间预约
      Reflect.set(paramInfo, 'searchType', 1);
      // 1:查询可取号信息 2:查询可退号信息
      Reflect.set(paramInfo, 'status', 1);
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'QueryRegisterRecord');
    },

    // 确定预约信息
    handleSureReservationInfo(row) {
      const self = this;
      let params = Object();
      let paramInfo = Object();
      let cardTypeInfo = this.cardTypeInfo;
      let patientInfo = this.patientInfo;
      let devInfo = JSON.parse(localStorage.getItem('deviceInfo'));
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'QueryRegisterRecord');
      if (patientInfo.state) {
        Reflect.set(paramInfo, 'patientId', (patientInfo.result.patientId));
      } else {
        Reflect.deleteProperty(paramInfo, 'patientId');
      }

      Reflect.set(paramInfo, 'patientId', patientInfo?.result?.patientId);
      Reflect.set(paramInfo, 'cardNo', cardTypeInfo?.cardNo || cardTypeInfo?.cardNumber);
      Reflect.set(paramInfo, 'cardType', cardTypeInfo?.id);
      Reflect.set(paramInfo, 'regMode', 1);
      Reflect.set(paramInfo, 'appoNo', row?.appoNo);
      Reflect.set(paramInfo, 'orderNo', row?.orderNo);
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'TakeAccount');
    },

    // 弹层-展开
    handleShown(row) {
      let info = Object();
      Reflect.set(info, 'position', "bottom");
      Reflect.set(info, 'overlay', false);
      Reflect.set(info, 'round', true);
      Reflect.set(info, 'row', row);
      Reflect.set((this.takeNumberInfo), 'visible', true);
      Reflect.set((this.takeNumberInfo), 'info', info);
    },

    // 弹层取消-关闭
    handleCancel() {
      this.$store.dispatch('DRCbiz/setCTDTimeInfo', 90000);
      Reflect.set((this.takeNumberInfo), 'visible', false)
    },

    // 弹层确定-关闭(取消已经挂的号)
    handleSureCancel(row) {
      const self = this;
      let params = Object();
      let paramInfo = Object();
      let cardTypeInfo = this.cardTypeInfo;
      let patientInfo = this.patientInfo;
      let devInfo = JSON.parse(localStorage.getItem('deviceInfo'));
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'CancelAppointOrRegister');
      if (patientInfo.state) {
        Reflect.set(paramInfo, 'patientId', (patientInfo.result.patientId));
      } else {
        Reflect.deleteProperty(paramInfo, 'patientId');
      }
      Reflect.set(paramInfo, 'cardNo', cardTypeInfo?.cardNo || cardTypeInfo?.cardNumber);
      Reflect.set(paramInfo, 'cardType', cardTypeInfo?.id);
      Reflect.set(paramInfo, 'regMode', 1);
      Reflect.set(paramInfo, 'appoNo', row?.appoNo);
      Reflect.set(paramInfo, 'orderNo', row?.orderNo);
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'CancelAppointOrRegister');
      this.handleCancel();
    }
  },

  watch: {
    // 步距
    stepInfo: {
      handler: function (val) {
        const self = this;
        self.widget = uuid();
        let cardTypeInfo = (this.cardTypeInfo);
        // 非步骤2关闭扫码功能
        if (val === 2) {
          if (parseInt(cardTypeInfo?.id) === 29) {
            buildParent.executeParentMethod('MICode');
          } else {
            buildParent.executeParentMethod('openScan');
          }
        } else {
          buildParent.executeParentMethod('closeScan');
        }
        self.$store.dispatch('DRCbiz/setCTDTimeInfo', 90000);
        self.curCompName = Reflect.get(self.compsOptions, val);
      },
      deep: true,
      immediate: true,
    },
  }
}

</script>

<style scoped lang="scss">
@import "./assets/style";
</style>